package edu.pe.cayetano.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.commons.beanutils.DynaBean;

import ws.synopsis.frmwrk.common.BResult;
import edu.pe.cayetano.framework.Configuration;
import edu.pe.cayetano.framework.DAOException;
import edu.pe.cayetano.model.bean.ExamenClinicoGeneralBean;
import edu.pe.cayetano.util.Utilitario;

public class ExamenClinicoGeneralDAO extends BaseDAO{


	String 					schema			= Configuration.getSchemaDB();
	
	public ExamenClinicoGeneralDAO(Connection cnx) {
		super(cnx);
	}

	public BResult buscarECGEspecifico(String codigo) throws Exception {

		ExamenClinicoGeneralBean 	ecgTO	 	= null;
		PreparedStatement   		pstmt 		 	= null;
		ResultSet 					rs 			 	= null;
		BResult						ret				= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" SELECT");
			query.append(" FILEHC,APRECIACION_GENERAL,FACIES,GRADO_COLABORACION,PESO,TALLA,TEMPERATURA,LESIONES,");
			query.append(" TEMPERATURA_CORPORAL,PULSO,FRECUENCIA_RESPIRATORIA,PRESION_ARTERIAL,OBSERVACIONES");   
			query.append(" FROM "+schema+"EXAMEN_CLINICO_GENERAL"); 
			query.append(" WHERE FILEHC=?");
			System.out.println("ExamenClinicoGeneralDAO:: buscarECGEspecifico:: "+query.toString());
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,codigo.substring(2,codigo.length()));
			rs=pstmt.executeQuery();
			ret = new BResult();
			if(rs.next()){
				ecgTO=new ExamenClinicoGeneralBean();
				ecgTO.setFilehc(rs.getString(1));
				ecgTO.setApreciacion_general(rs.getString(2));
				ecgTO.setFacies(rs.getString(3));
				ecgTO.setGrado_colaboracion(rs.getString(4));
				ecgTO.setPeso(rs.getString(5));
				ecgTO.setTalla(rs.getString(6));
				ecgTO.setTemperatura(rs.getString(7));
				ecgTO.setLesiones(rs.getString(8));
				ecgTO.setTemperatura_corporal(rs.getString(9));
				ecgTO.setPulso(rs.getString(10));
				ecgTO.setFrecuencia_respiratoria(rs.getString(11));
				ecgTO.setPresion_arterial(rs.getString(12));
				ecgTO.setObservaciones(rs.getString(13));
				ret.setObject(ecgTO);
				ret.setCode(0);
			}else{
				ecgTO=new ExamenClinicoGeneralBean();
				ret.setObject(ecgTO);
				ret.setCode(1);
			}
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(-1);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
				rs.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	}

	public BResult editaECGArchivoHC(DynaBean dyna) throws Exception {
		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" UPDATE "+schema+"EXAMEN_CLINICO_GENERAL SET ");
			query.append(" APRECIACION_GENERAL=?,FACIES=?,GRADO_COLABORACION=?,PESO=?,TALLA=?,TEMPERATURA=?,LESIONES=?,");
			query.append(" TEMPERATURA_CORPORAL=?,PULSO=?,FRECUENCIA_RESPIRATORIA=?,PRESION_ARTERIAL=?,OBSERVACIONES=?");   
			query.append(" WHERE FILEHC=?"); 
			String filehc = Utilitario.getPropertyDynaBean(dyna,"filehc");
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,Utilitario.getPropertyDynaBean(dyna,"apreciacion_general"));	 	
			pstmt.setString(2,Utilitario.getPropertyDynaBean(dyna,"facies"));	 	
			pstmt.setString(3,Utilitario.getPropertyDynaBean(dyna,"grado_colaboracion"));	 	
			pstmt.setString(4,Utilitario.getPropertyDynaBean(dyna,"peso"));	 	
			pstmt.setString(5,Utilitario.getPropertyDynaBean(dyna,"talla"));	 	
			pstmt.setString(6,Utilitario.getPropertyDynaBean(dyna,"temperatura"));	 	
			pstmt.setString(7,Utilitario.getPropertyDynaBean(dyna,"lesiones"));	
			pstmt.setString(8,Utilitario.getPropertyDynaBean(dyna,"temperatura_corporal"));		
			pstmt.setString(9,Utilitario.getPropertyDynaBean(dyna,"pulso"));		
			pstmt.setString(10,Utilitario.getPropertyDynaBean(dyna,"frecuencia_respiratoria"));		
			pstmt.setString(11,Utilitario.getPropertyDynaBean(dyna,"presion_arterial"));		
			pstmt.setString(12,Utilitario.getPropertyDynaBean(dyna,"observaciones"));	
			pstmt.setString(13,filehc.substring(2,filehc.length()));
			pstmt.executeUpdate();	
			ret = new BResult();
			ret.setCode(0);
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(-1);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	} 

	public BResult insertaECGArchivoHC(DynaBean dyna) throws Exception {

		BResult					ret				= null;
		PreparedStatement		pstmt 		 	= null;
		try{		
			StringBuffer query  = new StringBuffer();
			query.append(" INSERT INTO "+schema+"EXAMEN_CLINICO_GENERAL VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); 
			String filehc = Utilitario.getPropertyDynaBean(dyna,"filehc");
			pstmt = getConnection().prepareStatement(query.toString());
			pstmt.setString(1,filehc.substring(2,filehc.length()));
			pstmt.setString(2,Utilitario.getPropertyDynaBean(dyna,"apreciacion_general"));	 	
			pstmt.setString(3,Utilitario.getPropertyDynaBean(dyna,"facies"));	 	
			pstmt.setString(4,Utilitario.getPropertyDynaBean(dyna,"grado_colaboracion"));	 	
			pstmt.setString(5,Utilitario.getPropertyDynaBean(dyna,"peso"));	 	
			pstmt.setString(6,Utilitario.getPropertyDynaBean(dyna,"talla"));	 	
			pstmt.setString(7,Utilitario.getPropertyDynaBean(dyna,"temperatura"));	 	
			pstmt.setString(8,Utilitario.getPropertyDynaBean(dyna,"lesiones"));	
			pstmt.setString(9,Utilitario.getPropertyDynaBean(dyna,"temperatura_corporal"));		
			pstmt.setString(10,Utilitario.getPropertyDynaBean(dyna,"pulso"));		
			pstmt.setString(11,Utilitario.getPropertyDynaBean(dyna,"frecuencia_respiratoria"));		
			pstmt.setString(12,Utilitario.getPropertyDynaBean(dyna,"presion_arterial"));		
			pstmt.setString(13,Utilitario.getPropertyDynaBean(dyna,"observaciones"));	
			pstmt.setInt(14,1);	
			pstmt.setString(15,"");	
			pstmt.executeUpdate();	
			ret = new BResult();
			ret.setCode(0);
		}catch(Exception e){
			ret = new BResult();
			ret.setCode(-1);
			ret.setMessage(e.getMessage());
		}finally{
			try{
				pstmt.close();
			}
			catch(Exception e){
				throw new DAOException(e.getMessage());	
			}
		}
		return ret;
	} 
}
